package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {

    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecParamDTO> querySpecParams(Long categoryId, Long groupId, Boolean searching) {
        // 健壮性判断
        if(categoryId ==null && groupId ==null){
            throw new LyException(400,"分类参数或规格组参数至少填写一个！");
        }
            //select *
        List<SpecParam> list = specParamService.query()
                // from tb_spec_param p
                // where p.category_id = 76 and p.group_id = 1 and searching = 0;
                .eq(categoryId!=null,"category_id",categoryId)
                .eq(groupId!=null,"group_id",groupId)
                .eq(searching!=null,"searching",searching).list();
        return list.stream().map(SpecParamDTO::new).collect(Collectors.toList());
    }
}